package com.wxtt.android.fragment;

import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.fast.dev.frame.Adapter.listview.BaseListAdapter;
import com.fast.dev.frame.bean.I_Model;
import com.wxtt.android.R;
import com.wxtt.android.common.CommonFragment;
import com.wxtt.android.view.SwipeRefresh;
import com.wxtt.android.widget.EmptyBridge;

/**
 * 说明：BaseListFragment
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/8 10:21
 * <p/>
 * 版本：verson 1.0
 */
public abstract class BaseListFragment<Bean extends I_Model> extends CommonFragment implements SwipeRefresh.RefreshListener, AdapterView.OnItemClickListener {

    protected ListView lv;
    protected EmptyBridge empty_bridge;
    protected FloatingActionButton fab_to_top;
    //下拉刷新
    protected SwipeRefresh swipe_refresh;
    //设置Adapter
    protected BaseListAdapter<Bean> mAdapter;
//    private boolean scrollFlag = false;//标记是否滑动

    @Override
    protected int setRootView() {
        return R.layout.fragment_baselist;
    }

    @Override
    protected void onInit(View view) {
        super.onInit(view);
        //初始化控件
        initWidght();
        //初始化下拉刷新组件
        initSwipeRefresh();
        lv.setOnItemClickListener(this);
        if (mAdapter == null){
            mAdapter = setAdapter();
        }
        lv.setAdapter(mAdapter);
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
//                switch (scrollState){
//                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING://是当用户由于之前划动屏幕并抬起手指，屏幕产生惯性滑动时
////                        scrollFlag = false;
//                        break;
//                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://不滚动
////                        scrollFlag = false;
//
//                        break;
//                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://滚动中
////                        scrollFlag = true;
//                        break;
//                }
                //判断滚动到顶部
                if (view.getFirstVisiblePosition() == 0){
                    fab_to_top.setVisibility(View.GONE);
                }else {
                    fab_to_top.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });
    }

    /**
     * 说明：初始化下拉刷新组件
     */
    private void initSwipeRefresh() {
        swipe_refresh.setRefreshListener(this);
    }

    /**
     * 说明：初始化控件
     */
    private void initWidght(){
        empty_bridge = bind(R.id.empty_bridge);
        lv = bind(R.id.lv);
        swipe_refresh = bind(R.id.swipe_refresh);
        fab_to_top = bind(R.id.fab_to_top);

        fab_to_top.setOnClickListener(this);
    }

    @Override
    protected void clickView(View v) {
        super.clickView(v);
        switch (v.getId()){
            case R.id.fab_to_top:
                if (lv != null && swipe_refresh != null){
                    if (lv.getCount() >= 40){
                        lv.setSelection(0);
                    }else {
                        lv.smoothScrollToPosition(0);
                    }
                    swipe_refresh.autoRefresh();
                    fab_to_top.setVisibility(View.GONE);
                }
                break;
        }
    }

    /**
     * 说明：获取数据
     * @return
     */
    public abstract BaseListAdapter<Bean> setAdapter();


}
